<template>
  <div class="add_ress">
      <nav-bar>
           <template #left>
              <van-icon name="arrow-left" @click="backHome" />
          </template>
        <template #center> 
            地址管理
          </template>
      </nav-bar>
      <div class="adr_con">
        <van-address-list
        v-model="chosenAddressId"
        :list="list"
        default-tag-text="默认"
        @add="onAdd"
        @edit="onEdit"
      />
      </div>
  </div>
</template>

<script>
import { reactive, toRefs } from '@vue/reactivity';
import { useRouter } from 'vue-router'
import {useStore} from 'vuex'
import NavBar from '../components/common/NavBar.vue'
import { onMounted } from '@vue/runtime-core';
export default {
  name:'Address',
  components:{
    NavBar
  },
  setup(){
    const store = useStore();
     const router = useRouter();
     const data = reactive({
       chosenAddressId:0,
       list:[],
     })
    //  路由跳转
     const backHome = ()=>{
       router.back();
     }
    //  初始化数据
    const init= ()=>{
      data.list = store.state.useAddress;
    }
    onMounted(()=>{
      init();
    })
    //  新增地址
     const onAdd =()=>{
         router.push({
          path:'/newaddress',
        query:{
          type:"add"
        }
      })
     }
    //  编辑地址
     const onEdit = (item)=>{
      data.chosenAddressId = item.id
      router.push({
        path:'/newaddress',
        query:{
          id:item.id,
          type:"change"
        }
      })
     }
     return {
       ...toRefs(data),
       backHome,
        onAdd,
        onEdit
     }
  }
}
</script>

<style scoped lang="less">
 /deep/ .van-button--danger {
   background-color: #ffc400;
   border-color: #ffc400;
 }
 .add_ress{
   height: 100vh;
   background-color: #f4f4f4;
 }
</style>